package cn.offcn.controller;

import cn.offcn.entity.User;
import cn.offcn.service.UserService;
import cn.offcn.utils.OAResult;
import cn.offcn.utils.PageView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.annotation.RequestScope;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Value("${pageSize}")
    private int pageSize;
    @RequestMapping("/{page}")
    public String forwardPage(@PathVariable("page") String page, Integer uid, Model model){

        model.addAttribute("uid",uid);
        return "user/"+page;
    }

    @RequestMapping("/list")
    @ResponseBody
    public PageView<User> getUsers(@RequestParam(defaultValue = "1") int currentPage,String uname){

        return userService.getUsers(currentPage,pageSize,uname);
    }

    @RequestMapping("/deleteUserById")
    @ResponseBody
    public OAResult deleteUserById(int uid){

       return  userService.deleteUserById(uid);
    }

    @RequestMapping("/getUserById")
    @ResponseBody
    public User getUserById(int uid){

       return userService.getUserById(uid);

    }
}
